package com.pdf.utils;

import com.domain.PdfUser;
import com.fonts.AbstractFont;
import com.fonts.DefaultFont;
import com.itextpdf.text.*;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import lombok.Data;

import javax.swing.*;
import java.awt.*;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.Random;

/**
 * 文字水印
 */
@Data
public class Watermark extends AbstractWatermark {
    public static final Rectangle A4 = new RectangleReadOnly(842, 595);
    private BaseFont baseFont;
    private String text;

    public Watermark(Class baseFontClassName) {
        this(baseFontClassName.getName(), "shuizhongyou");
    }

    public Watermark(Class baseFontClassName, String text) {
        this(baseFontClassName.getName(), text);
    }

    public Watermark() {
        this(DefaultFont.class.getName(), "shuizhongyou");
    }

    public Watermark(String text) {
        this(DefaultFont.class.getName(), text);
    }

    public Watermark(String baseFontClassName, String text) {
        this.baseFont = AbstractFont.chooseFont(baseFontClassName).getBaseFont();
        this.text = text;
    }

    @Override
    public void addWatermark(String fileName, OutputStream outputStream, Boolean lock, PdfUser pdfUser) {
        BufferedOutputStream out = null;
        PdfReader reader = null;
        PdfStamper stamper = null;
        FileInputStream fileInputStream = null;
        File file = new File(fileName);
        try {
            fileInputStream = new FileInputStream(file);
            out = new BufferedOutputStream(outputStream);
            if (lock) {
                reader = new PdfReader(fileInputStream, pdfUser.getAdminPassword().getBytes());
                stamper = new PdfStamper(reader, out);
                pdfUser.lock(stamper, pdfUser.getUserPassword(), pdfUser.getAdminPassword());
            }else {
                reader = new PdfReader(fileInputStream);
                stamper = new PdfStamper(reader, out);
            }

            int total = reader.getNumberOfPages() + 1;
            System.out.println("当前总页数：" + total);
            PdfContentByte under = null;
            Random random = new Random();
            for (int i = 1; i < total; i++) {
                // 水印的起始
                under = stamper.getOverContent(i);
                // 新的一页被创建后添加水印
                PdfGState gs = new PdfGState();
                //这里是透明度设置,数值越大越不透明
                gs.setFillOpacity(0.2f);
                //这里是条纹不透明度
                gs.setStrokeOpacity(0.4f);

                JLabel label = new JLabel();
                FontMetrics metrics;
                label.setText(text);
                metrics = label.getFontMetrics(label.getFont());
                int textH = metrics.getHeight();
                int textW = metrics.stringWidth(label.getText());

                under.saveState();
                under.setGState(gs);
                under.beginText();
                under.setFontAndSize(baseFont, 20);
                under.setRGBColorFill(145, 145, 145);

                // 页面填充水印

                for (float initX = random.nextInt(200) - 150; initX <= A4.getWidth() + 40; initX += 300) {
                    for (float initY = random.nextInt(200) - 150; initY <= A4.getHeight() + 40; initY += 300) {
                        under.showTextAligned(Element.ALIGN_LEFT, text, initX - textW, initY - textH, 30); // rotation是水印倾斜角度
                    }
                }
                under.endText();
                under.restoreState();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != stamper) {
                    stamper.close();
                }
                if (null != out) {
                    out.close();
                }
                if (null != reader) {
                    reader.close();
                }
                if (null != file) {
                    file.delete();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
